﻿using Azure.Messaging;
using MESMian.Common;
using MESMian.Context;
using Microsoft.EntityFrameworkCore;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MESMian.Forms.ProductionSpec
{
    public partial class ProductionSpecAddEdit : Form
    {
        private int editId = 0;
        ProductSpecs? editModel;
        public ProductionSpecAddEdit(int editId)
        {
            InitializeComponent();
            this.editId = editId;
            Load += ProductionSpecAddEdit_Load;
            btnSave.Click += BtnSave_Click;
        }



        private void ProductionSpecAddEdit_Load(object? sender, EventArgs e)
        {
            if (editId > 0)
            {
                Text = "产品规格-编辑";
                using (var mtx = new MyContext())
                {
                    editModel = mtx.ProductSpecs.Find(editId);
                    if (editModel != null)
                    {
                        txtProductSpecName.Text = editModel.ProductSpecName;
                    }
                }
            }
            else
            {
                Text = "产品规格-添加";
            }
        }


        private void BtnSave_Click(object? sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtProductSpecName.Text))
            {
                MessageBox.Show("产品类型名称不能为空！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            using (var mtx = new MyContext())
            {
                var model = new ProductSpecs()
                {
                    ProductSpecName = txtProductSpecName.Text
                };

                if (editId > 0)
                {
                    if (editModel != null)
                    {
                        model = editModel;
                        var query = mtx.ProductCategories.Where(item =>
                        item.ProductCategoryName == txtProductSpecName.Text &&
                        item.ProductCategoryName != editModel.ProductSpecName);
                        if (query.Count() >= 1)
                        {
                            MessageBox.Show("产品类型名称重复！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                        editModel.ProductSpecName = txtProductSpecName.Text;
                        editModel.LastUpdateUserId = LoginInfo.Id;
                        editModel.LastUpdateTime = DateTime.Now;
                        mtx.Entry(model).State = EntityState.Modified;
                    }
                }
                else
                {
                    var query = mtx.ProductCategories.Where(item =>
                        item.ProductCategoryName == txtProductSpecName.Text);
                    if (query.Count() >= 1)
                    {
                        MessageBox.Show("产品规格名称重复！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    mtx.Entry(model).State = EntityState.Added;
                }
                mtx.SaveChanges();
                DialogResult = DialogResult.OK;
            }

        }
    }
}

